package cn.android_mobile.core;


import java.io.File;
import java.util.Stack;

import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Environment;
import cn.android_mobile.core.database.DBHelper;
import cn.android_mobile.toolkit.CacheUtil;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.umeng.analytics.MobclickAgent;
import com.ygsoft.smartfast.android.BaseApplication;

public class BasicApplication extends BaseApplication {

	public final static String TAG = "PateoApplication";
	
	/** 数据库连接类 */
	public static DBHelper dbHelper = null;
	public static SharedPreferences mPref;
	public static int helpFlag = -1;

	// 用于格式化日期,作为日志文件名的一部分
	public static Stack<Activity> activityStack = new Stack<Activity>();
	public static Context context;
	public static final String FNAME = "CoreLib";
	
	

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onCreate() {

		super.onCreate();
		MobclickAgent.openActivityDurationTrack(false);
		MobclickAgent.setDebugMode(true);
		mPref = getSharedPreferences("peato_sp", Context.MODE_PRIVATE);
		context = getApplicationContext();
		// CrashHandler crashHandler = CrashHandler.getInstance();
		// Thread.setDefaultUncaughtExceptionHandler(this);

		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.cacheInMemory(true).cacheOnDisc(true).build();
		 //图片保存系统路径  
        File cacheDir = StorageUtils.getOwnCacheDirectory(this, FNAME);  
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.defaultDisplayImageOptions(defaultOptions)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCacheSizePercentage(5)
				// 20%
				.discCache(
						new TotalSizeLimitedDiscCache(cacheDir,
								200 * 1024 * 1024/* 100M */))
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				// .writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		FinalBitmap finalBitmap = FinalBitmap.create(getApplicationContext());
		finalBitmap.configLoadingImage(R.drawable.image_loading);
		finalBitmap.configLoadfailImage(R.drawable.image_loading);
		
		ImageLoader.getInstance().init(config);
		CacheUtil.context = this.getApplicationContext();
		helpFlag = CacheUtil.getInteger("app_help_view");
	}

	public static synchronized DBHelper getDBHelper() {
		if (dbHelper == null) {
			dbHelper = OpenHelperManager.getHelper(context, DBHelper.class);
		}
		return dbHelper;
	}

	public static synchronized void destroyDataHelper() {
		if (null != dbHelper) {
			OpenHelperManager.releaseHelper();
			dbHelper = null;
		}
	}

	@Override
	public void onTerminate() {
		destroyDataHelper();
		super.onTerminate();
	}

	/**
	 * 
	 * @param context
	 */
	public static void initImageLoader(Context context) {
		// DisplayImageOptions options = new DisplayImageOptions.Builder()
		// .showImageOnLoading(R.drawable.com_default_pic)
		// .showImageForEmptyUri(R.drawable.com_default_pic)
		// .showImageOnFail(R.drawable.com_default_pic).cacheInMemory(true)
		// .cacheOnDisc(true).build();
		// ImageLoaderConfiguration config = new
		// ImageLoaderConfiguration.Builder(context)
		// .threadPriority(Thread.NORM_PRIORITY - 2)
		// .denyCacheImageMultipleSizesInMemory()
		// .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
		// .memoryCacheSize(2 * 1024 * 1024).discCacheSize(50 * 1024 * 1024)
		// .discCacheFileCount(100).defaultDisplayImageOptions(options)
		// .discCacheFileNameGenerator(new Md5FileNameGenerator())
		// .tasksProcessingOrder(QueueProcessingType.LIFO).build();
		// ImageLoader.getInstance().init(config);
	}

	public String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();

	}
}
